package com.thread;

import com.framework.config.OcrConfig;
import com.google.common.collect.Lists;
import com.ocr.baidu.BaiduOCRUtils;
import com.utils.EasyPoiUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 文件名称： com.thread.BaiduOcrThread.java</br>
 * 初始作者： AirOrangeWorkSpace</br>
 * 创建日期： 2020/12/25 11:02</br>
 * 功能说明： 百度OCR 处理 <br/>
 * =================================================<br/>
 * 修改记录：<br/>
 * 修改作者        日期       修改内容<br/>
 * ================================================<br/>
 * Copyright (c) 2020-2021 .All rights reserved.<br/>
 */
@Data
@Slf4j
public class BaiduOcrThread implements Runnable {
    private OcrConfig ocrConfig;

    public BaiduOcrThread(OcrConfig ocrConfig) {
        this.ocrConfig = ocrConfig;
    }

    @Override
    public void run() {
        long threadStartTime = System.currentTimeMillis();
        log.info("-------------------百度接口处理程序还在启动-------------------");
        String ocrFolderPath = ocrConfig.getOcrFolderPath();
        String ocrFile = ocrConfig.getOcrFile();
        OcrConfig.BaiduOcr baiduOcr = ocrConfig.getBaiduOcr();
        boolean useToken = baiduOcr.isUseToken();
        boolean useIdCard = baiduOcr.isUseIdCard();
        List<File> allFiles = Lists.newArrayList();
        File singleFile = new File(ocrFile);
        if (singleFile.exists() && singleFile.isFile()) {
            allFiles.add(singleFile);
        }
        File folderPath = new File(ocrFolderPath);
        if (folderPath.exists() && folderPath.isDirectory()) {
            // 只取图片类文件 png jpg
            func(folderPath, allFiles);
        }
        int imageCount = allFiles.size();
        log.info("共加载图片" + imageCount + "张");
        List<Map<String, Object>> ocrResult = new ArrayList<>();
        for (File allFile : allFiles) {
            String filePath = allFile.getAbsolutePath();
            JSONObject res;
            if (useToken) {
                if (useIdCard){
                    res = new JSONObject(BaiduOCRUtils.idCardByToken(baiduOcr, filePath));
                }else {
                    res = new JSONObject(BaiduOCRUtils.generalBasicByToken(baiduOcr, filePath));
                }
            } else {
                if (useIdCard){
                    res = BaiduOCRUtils.idCardBySdk(baiduOcr, filePath);
                }else {
                    res = BaiduOCRUtils.generalBasicBySdk(baiduOcr, filePath);
                }
            }
            log.info("进行一次百度OCR");
            Map<String, Object> stringObjectMap = res.toMap();
            ocrResult.add(stringObjectMap);
        }
        if (ocrResult.size() > 0) {
            try {
                EasyPoiUtil.exportBigExcel("OCR结果", ocrResult);
            } catch (Exception e) {
                log.error(e.getMessage());
                log.info("OCR结果导出失败");
            }
        }
        log.info("程序 总耗时" + (System.currentTimeMillis() - threadStartTime) / 1000 + "S");
        log.info("OCR 工作结束------------------------------");
    }

    private static void func(File file, List<File> fileList) {
        File[] fs = file.listFiles();
        for (File f : fs) {
            if (f.isDirectory()) {
                //若是目录，则递归打印该目录下的文件
                func(f, fileList);
            }
            if (f.isFile()) {
                String imageName = f.getName();
                boolean isJpg = imageName.endsWith(".jpg");
                boolean isJPG = imageName.endsWith(".JPG");
                boolean isPng = imageName.endsWith(".png");
                if (isJPG || isPng || isJpg) {
                    //若是图片加入列表
                    fileList.add(f);
                }
            }
        }
    }
}
